package com.team.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    /*
     * preHandle,拦截请求时执行的方法(进入控制器之前)
     * 注意：该方法接收返回值，如果返回true,则表示放行可以进入控制器
     *      反之返回false表示不放行，不能进入控制器
     * */

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        HttpSession session=request.getSession();
        Object ov=session.getAttribute("userInfo");
        if(ov!=null){
            return true;  //放行
        }else {
            //如果没有session
            //跳转到登入页
            //response.sendRedirect("/jsp/devlogin.jsp");
            response.sendRedirect("/index");  //控制器请求进入登入页
            return false;
        }
    }
    @Override
    //postHandle在业务处理器处理请求执行完成后(控制器执行完后),生成视图之前执行;
    // System.out.println("postHandle.......");

    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    //afterCompletion在DispatcherServlet完全处理完请求后被调用,可用于清理资源等 afterCompletion()执行完成后开始渲染页面
    // System.out.println("afterCompletion");
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
